/***
 * @param ceet@vip.qq.com
 * @param DES加密解密
 */
import cryptoJs from 'crypto-js';

// DES加密-CBC模式
export const encryptDes = (message, key, iv) => {
	var keyHex = cryptoJs.enc.Utf8.parse(key);
	var ivHex = cryptoJs.enc.Utf8.parse(iv);
	var option = {
		iv: ivHex,
		mode: cryptoJs.mode.CBC,
		padding: cryptoJs.pad.Pkcs7
	}
	var encrypted = cryptoJs.DES.encrypt(message, keyHex, option);
	return encrypted.ciphertext.toString();
}

// DES解密-CBC模式
export const decryptDes = (message, key, iv) => {
	var keyHex = cryptoJs.enc.Utf8.parse(key);
	var ivHex = cryptoJs.enc.Utf8.parse(iv);
	var decrypted = cryptoJs.DES.decrypt({
			ciphertext: cryptoJs.enc.Hex.parse(message)
		},
		keyHex, {
			iv: ivHex,
			mode: cryptoJs.mode.CBC,
			padding: cryptoJs.pad.Pkcs7
		}
	)
	return decrypted.toString(cryptoJs.enc.Utf8);
}